import {Component, ViewChild} from '@angular/core';
import {Content, InfiniteScroll, IonicPage, NavController} from 'ionic-angular';
import {WorkerProvider} from "@providers/interface/worker";
import {StorageProvider} from "@providers/storage";
import {DialogProvider} from "@providers/dialog";
import {PagingConfig} from "@providers/paging";
import {Util} from "@providers/util";
import {NativeProvider} from "@providers/natives/native";

@IonicPage()
@Component({
  selector: 'page-worker-take',
  templateUrl: 'worker-take.html',
})
export class WorkerTakePage {

  datas: any = [];//数据
  datasConfig: PagingConfig = new PagingConfig();
  isScrollDown = false;//判断页面是否是向下滑动
  userInfo : any ;
  isApp: any = this.native.isCordova;
  @ViewChild(Content) content: Content;

  constructor(private worker: WorkerProvider,
              private storage: StorageProvider,
              private navCtrl: NavController,
              private dialog: DialogProvider,
              private native: NativeProvider,) {
    this.userInfo = this.storage.read("userInfo");
  }


  /**
   * 滑动时触发
   */
  scrollContent() {
    this.isScrollDown = this.content.directionY == "down";
  }

  ionViewWillEnter() {
    this.userInfo = this.storage.read("userInfo");
    this.datasConfig = new PagingConfig();
    this.getData(data => {
      this.datas = data;
    }, this.datasConfig);
  }

  /**
   * 下拉刷新
   * @param refresher
   */
  doRefresh(refresher?: any) {
    this.datasConfig = new PagingConfig();
    this.getData(data => {
      this.datas = data;
      if (refresher) {
        setTimeout(() => {
          refresher.complete();
        }, 500);
      }
      console.log(this.datas)
    }, this.datasConfig, () => {
      if (refresher) {
        setTimeout(() => {
          refresher.complete();
        }, 500);
      }
      this.dialog.showAlert('刷新失败，请检查网络！');
    });

  }

  /**
   * 上拉刷新
   * @param infiniteScroll
   */
  getMoreDatas(infiniteScroll: InfiniteScroll) {
    this.getData((value) => {
      this.datas.push.apply(this.datas, value);
      infiniteScroll.complete();
    }, this.datasConfig);
  }

  /**
   * 获取数据
   * @param pagingConfig 分页
   * @param fn 方法
   * @param errFun 反生错误后执行
   */
  getData(fn: (value) => void, pagingConfig, errFun?: () => void) {
    let userInfo = this.storage.read("userInfo");
    this.worker.selectSOrder(
      {"STATUSID": "GRABED", "PARTYID": userInfo['PARTYID']}, pagingConfig)
      .then(data => {
        data = data || [];
        for (let i = 0; i < data.length; i++) {
          let v = data[i];
          let d: Date = new Date(v.THRUTIME);
          if (!isNaN(v.THRUTIME)) {
            v.THRUTIME = Util.dateFormat(d, "yyyy-MM-dd")
          }
          if(v.GUARANTEE == 1) {
            v.GUARANTEE = '保内';
          }else if(v.GUARANTEE == 2){
            v.GUARANTEE = '保外';
          }else if(v.GUARANTEE == 3){
            v.GUARANTEE = '延保';
          }else {
            v.GUARANTEE = '无';
          }
        }
        fn(data);
      })
      .catch(e => {
        if (errFun) {
          errFun();
        }
      });
  }

  /**
   * 接单
   * @param data
   * @param i
   */
  submit(data: any, i: number) {
    this.dialog.showConfirm(() => {
      this.dialog.showLoading();
      this.worker.dispatch(data)
        .then(v => {
          this.dialog.hideLoading();
          this.datas.splice(i, 1);
        })
        .catch(e => {
          this.dialog.hideLoading();
          this.dialog.showAlert('该订单已经被其他人抢了，您可以选择其他订单！');
          this.doRefresh();
        });
    }, "是否确定抢单？");
  }

  /**
   * 开工
   */
  switchIsWorking() {
    this.worker.switchIsWorking(this.userInfo["PARTYID"], '10')
      .then(r => {
        this.userInfo['SFZX'] = r.data["SFZX"];
        this.storage.write('userInfo', this.userInfo);
      })
      .catch(e => {
      });
  }

  goToMap() {
    this.navCtrl.push("WorkerTakeMapPage");
  }
}
